<?php
    //include "../database.php";
    //db_connect();

    //For form to send data
    if(empty($page)) {
	$page = "addSubdivisionsToMeasure.php";
    }

    $sql = "SELECT * FROM subdivisions ORDER BY code";
    $subdivisions = mysql_query($sql);

    $sql = "SELECT code, title FROM support ORDER BY code";
    $support_measures = mysql_query($sql);

    if(!empty($_POST['subdivision'])) {
	$measure = mysql_real_escape_string($_POST['measure']);
	$date = mysql_real_escape_string($_POST['date']);
	$subdivision = $_POST['subdivision'];

	//get existing subdivisions for this measure
	for($i = 0; $i < count($subdivision); $i++) {
	    $subdivision[$i] =  mysql_real_escape_string($subdivision[$i]);
	}
	$query = "SELECT subdivision_code FROM  support_administration WHERE measure_code = '$measure'";
	print $query;
	$subdivision_codes = mysql_query($query);

	//select only new subdivisions for this measure
	while($i = mysql_fetch_assoc($subdivision_codes)) {
	    if(in_array($i['subdivision_code'], $subdivision)) {
		$key = array_search($i['subdivision_code'], $subdivision);
		unset($subdivision[$key]);
	    }
	}
	if(count($subdivision) > 0) {
	    $sql = "INSERT INTO  support_administration (measure_code, subdivision_code, time, date) VALUES ";
	    $first = "";
	    foreach($subdivision as $i) {
		$sql .= "$first('$measure', '$i', '".mysql_real_escape_string($_POST['hours_'.$i])."', '$date')";
		$first = ", ";
	    }
	    mysql_query($sql);
	    print $sql;
	}
    }

    //db_close();
?>

<label for="error" style="color:red;"><?php if(!empty($error_string)) echo $error_string; ?></label>
<label for="inform" style="color:green;"><?php if(!empty($msg)) echo $msg; ?></label>

<form name="SubdivisionsToMeasure" action="<?php echo $page;?>" method="POST">
    <label for="measure">Paramos priemonė:</label>
    <!-- measures select box -->
    <select name="measure">
	<?php
	    while($support_measure = mysql_fetch_assoc($support_measures)) {
		print "<option value='$support_measure[code]'>$support_measure[code] : $support_measure[title]</option>";
	    }
	?>
    </select><br/>

    <label for="addSubdivision">Pridėti padalinius(jau esamų nebereikia):</label><br/>
    <!-- Subdivisions checkbox list -->
    <?php
	while($subdivision = mysql_fetch_assoc($subdivisions)) {
	    ?>
	    <label for="<?php print $subdivision['code']; ?>"><?php print $subdivision['code']; ?></label>
	    <input type="checkbox" name="subdivision[]" value="<?php print $subdivision['code']; ?>"/>
	    <label for="<?php print $subdivision['code']; ?>_hours">Val:</label>
	    <select name="hours_<?php print $subdivision['code']; ?>">
		<option value="1">1</option>
		<option value="2">2</option>
		<option value="3">3</option>
		<option value="4">4</option>
		<option value="5">5</option>
		<option value="6">6</option>
		<option value="7">7</option>
		<option value="8">8</option>
	    </select>
	    <?php
	}
    ?>
    <br/>

    <label for="SubdivisionAddDate">Padalinio pridėjimo/veiklos pradžios data:</label>
    <input type="" name="date" value="<?php print date("Y-m-d"); ?>"/><br/>

    <input type="submit" name="submit" value="Susieti"/>
</form>
<a href="index.php?changes">Atgal</a>